#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author:cooler

@create:2018-8-01 16:55

"""
'''
yum install -y cyrus-sasl-lib.x86_64 cyrus-sasl-devel.x86_64 libgsasl-devel.x86_64 saslwrapper-devel.x86_64  cyrus-sasl-plain

pip install  thrift_sasl pyhs2 sasl 

'''

import pyhs2
class HiveClient:
    def __init__(self, db_host, user, password, database, port=10000, authMechanism="PLAIN"):
        """
        create connection to hive server2
        """
        self.conn = pyhs2.connect(host=db_host,
                                  port=port,
                                  authMechanism=authMechanism,
                                  user=user,
                                  password=password,
                                  database=database,
                                  )
    def query(self, sql):
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            return cursor.fetch()
    def close(self):
        self.conn.close()

def main():
    hive_client = HiveClient(db_host='10.6.18.147', port=10000, user='vc', password='',
                             database='analysis', authMechanism='PLAIN')
    sql = '''
        SELECT userid,diff_city_1_month from ofo_diff_city_1_month  where dt='20180716' limit 100
    '''
    result = hive_client.query(sql)
    print result
    hive_client.close()

if __name__ == '__main__':
    main()

    